# Copyright 2015 Free Software Foundation, Inc.

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

standard_testfile "vla-sub.f90"

if { [prepare_for_testing ${testfile}.exp ${testfile} ${srcfile} \
    {debug f90 quiet}] } {
    return -1
}

if ![runto_main] {
    untested "could not run to main"
    return -1
}

# Check the values of VLA's in subroutine can be evaluated correctly

# Try to access values from a fixed array handled as VLA in subroutine.
gdb_breakpoint [gdb_get_line_number "not-filled"]
gdb_continue_to_breakpoint "not-filled (1st)"
gdb_test "print array1" " = \\(\[()1, .\]*\\)" \
  "print passed array1 in foo (passed fixed array)"

gdb_breakpoint [gdb_get_line_number "array1-filled"]
gdb_continue_to_breakpoint "array1-filled (1st)"
gdb_test "print array1(5, 7)" " = 5" \
  "print array1(5, 7) after filled in foo (passed fixed array)"
gdb_test "print array1(1, 1)" " = 30" \
  "print array1(1, 1) after filled in foo (passed fixed array)"

gdb_breakpoint [gdb_get_line_number "array2-almost-filled"]
gdb_continue_to_breakpoint "array2-almost-filled (1st)"
gdb_test "print array2" " = \\( *\\( *\\( *30, *3, *3,\[()3, .\]*\\)" \
  "print array2 in foo after it was filled (passed fixed array)"
gdb_test "print array2(2,1,1)=20" " = 20" \
  "set array(2,2,2) to 20 in subroutine (passed fixed array)"
gdb_test "print array2" " = \\( *\\( *\\( *30, *20, *3,\[()3, .\]*\\)" \
  "print array2 in foo after it was mofified in debugger (passed fixed array)"


# Try to access values from a fixed sub-array handled as VLA in subroutine.
gdb_continue_to_breakpoint "not-filled (2nd)"
gdb_test "print array1" " = \\(\[()5, .\]*\\)" \
  "print passed array1 in foo (passed sub-array)"

gdb_continue_to_breakpoint "array1-filled (2nd)"
gdb_test "print array1(5, 5)" " = 5" \
  "print array1(5, 5) after filled in foo (passed sub-array)"
gdb_test "print array1(1, 1)" " = 30" \
  "print array1(1, 1) after filled in foo (passed sub-array)"

gdb_continue_to_breakpoint "array2-almost-filled (2nd)"
gdb_test "print array2" " = \\( *\\( *\\( *30, *3, *3,\[()3, .\]*\\)" \
  "print array2 in foo after it was filled (passed sub-array)"
gdb_test "print array2(2,1,1)=20" " = 20" \
  "set array(2,2,2) to 20 in subroutine (passed sub-array)"
gdb_test "print array2" " = \\( *\\( *\\( *30, *20, *3,\[()3, .\]*\\)" \
  "print array2 in foo after it was mofified in debugger (passed sub-array)"


# Try to access values from a VLA passed to subroutine.
gdb_continue_to_breakpoint "not-filled (3rd)"
gdb_test "print array1" " = \\(\[()42, .\]*\\)" \
  "print passed array1 in foo (passed vla)"

gdb_continue_to_breakpoint "array1-filled (3rd)"
gdb_test "print array1(5, 5)" " = 5" \
  "print array1(5, 5) after filled in foo (passed vla)"
gdb_test "print array1(1, 1)" " = 30" \
  "print array1(1, 1) after filled in foo (passed vla)"

gdb_continue_to_breakpoint "array2-almost-filled (3rd)"
gdb_test "print array2" " = \\( *\\( *\\( *30, *3, *3,\[()3, .\]*\\)" \
  "print array2 in foo after it was filled (passed vla)"
gdb_test "print array2(2,1,1)=20" " = 20" \
  "set array(2,2,2) to 20 in subroutine (passed vla)"
gdb_test "print array2" " = \\( *\\( *\\( *30, *20, *3,\[()3, .\]*\\)" \
  "print array2 in foo after it was mofified in debugger (passed vla)"
